package com.haiertvbic.tvnow.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.haiertvbic.tvnow.util.SLog;

import android.content.Context;
import android.content.res.AssetManager;

public class InitAppDataBase {

	private static final String TAG = "InitAppDataBase";

	public boolean isDBExist(Context context) {
		// 查应用目录下是否有tvnowDB.db文件
		File tvnowDB = new File(context.getFilesDir().getParent()
				+ "/databases/tvnowDB.db");
		if (tvnowDB.exists()) {
			return true;
		} else {
			return false;
		}
	}

	public void copyDBFile(Context context) {
		// 先创建一个目录
		String dbDir = context.getFilesDir().getParent() + "/databases/";
		File dbFile = new File(dbDir);
		dbFile.mkdirs();
		AssetManager am = context.getAssets();
		// 下面是流的读写操作
		InputStream inputS = null;
		FileOutputStream outputS = null;
		try {
			inputS = am.open("tvnowDB.db");
			outputS = new FileOutputStream(dbDir + "tvnowDB.db");
			byte[] buffer = new byte[1024];
			int num;
			while ((num = inputS.read(buffer)) > 0) {
				outputS.write(buffer, 0, num);
			}
			outputS.close();
			inputS.close();
		} catch (IOException e) {
			SLog.i(TAG, e.toString());
		} catch (Exception e) {
			// 内存不够			
		}
	}

}
